//
//  POSActivationVC.m
//  NewPro
//
//  Created by 何荧红 on 2018/10/9.
//  Copyright © 2018 何荧红. All rights reserved.
//

#import "POSActivationVC.h"
#import "addAdressCell.h"

#import "ScanQRViewController.h"
#import "MinedoPosStore.h"
#import "BusinessManagerStore.h"

@interface POSActivationVC ()<UIScrollViewDelegate,UITableViewDelegate,UITableViewDataSource,ScanQRViewControllerDelegate>
@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, copy) NSString *customName;
@property (nonatomic, copy) NSString *posNnstanceSn;
@property (nonatomic, copy) NSString *customPhoneNumber;
@property (nonatomic, copy) NSString *code;
@property (nonatomic, copy) NSString *realCode;
@end

@implementation POSActivationVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initCSS];
}
- (void)initCSS{
    
    [self setNavBarWithTitle:@"MPOS激活登记"];
    
    UIScrollView *ScrollView = [[UIScrollView alloc]initWithFrame:CGRectZero];
    ScrollView.delegate = self;
    ScrollView.backgroundColor = [UIColor colorWithHexString:@"F1F1F1"];
    ScrollView.showsVerticalScrollIndicator = NO;
    ScrollView.showsHorizontalScrollIndicator = NO;
    ScrollView.contentSize = CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT);
    [self.view addSubview:ScrollView];
    
    [ScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.edges.equalTo(self.view);
    }];
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 200)];
    [self.tableView registerNib:[UINib nibWithNibName:@"addAdressCell" bundle:nil] forCellReuseIdentifier:@"addAdressCellID"];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.showsHorizontalScrollIndicator = NO;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.scrollEnabled = NO;
    [ScrollView addSubview:self.tableView];
    
    UIButton *toSure = [[UIButton alloc]init];
    toSure.layer.masksToBounds = YES;
    toSure.layer.cornerRadius = 5;
    [toSure addTarget:self action:@selector(toSureButton) forControlEvents:UIControlEventTouchUpInside];
    toSure.titleLabel.font = [UIFont fontWithName:@"PingFang-SC-Medium" size:18];
    [toSure setTitle:@"提交" forState:UIControlStateNormal];
    [toSure setTitleColor:[UIColor colorWithHexString:@"FFFFFF"] forState:UIControlStateNormal];
    toSure.backgroundColor = [UIColor colorWithHexString:@"0CA6F0"];
    [ScrollView addSubview:toSure];
    [toSure mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(ScrollView.mas_left).offset(14);
        make.width.mas_equalTo(SCREEN_WIDTH - 28);
        make.top.equalTo(self.tableView.mas_bottom).offset(91);
        make.height.mas_equalTo(44);
    }];
}
- (void)toSureButton{
    NSMutableDictionary *prams = [[NSMutableDictionary alloc]init];
    [prams setObject:[UserDefaultsManager sharedInstance].user_id forKey:@"posCustomId"];
    [prams setObject:@"1" forKey:@"region"];
    if (self.customName.length > 0) {
        [prams setObject:self.customName forKey:@"customName"];
        if ([self.customPhoneNumber isVaildMobileNo]) {
            [prams setObject:self.customPhoneNumber forKey:@"customPhoneNumber"];
            if (self.code.length > 0) {
                if ([self.code isEqualToString:self.realCode]) {
                    if (self.posNnstanceSn.length > 0) {
                        [prams setObject:self.posNnstanceSn forKey:@"posNnstanceSn"];
                        [self addmposDataWith:prams];
                    }else{
                        [self showMBPError:@"请输入机器序列号"];
                    }
                }else{
                    [self showMBPError:@"请输入正确验证码"];
                }
            }else{
                [self showMBPError:@"请输入验证码"];
            }
        }else{
            [self showMBPError:@"请输入正确手机号码"];
        }
    }else{
        [self showMBPError:@"请输入客户姓名"];
    }
}
#pragma mark - scan QR code delegate
//判断URL的类型
- (void)didScanSuccessWithResult:(NSString *)result {
    // 先将服务端返回的 URL 转义
    NSString *decodedString = [result stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    self.posNnstanceSn = decodedString;
    [self.tableView reloadData];
}
- (void)addmposDataWith:(NSDictionary *)dic{
    [[[BusinessManagerStore alloc]init] PostAddMPOSWithPrams:dic Success:^(NSDictionary * _Nonnull res) {
        if ([res[@"msg"] isEqualToString:@"success"]) {
            [self showMBPError:@"激活成功"];
            [self performSelector:@selector(back) withObject:nil afterDelay:1.0f];
        }else{
            [self showMBPError:res[@"msg"]];
        }
    } Failure:^(NSError * _Nonnull error) {
        [self showMBPError:[HttpTool handleError:error]];
    }];
}
- (void)back{
    [[AppDelegate sharedAppDelegate] popViewController];
}
- (void)sendCodeWith:(NSIndexPath *)index{
    [[[MinedoPosStore alloc]init] sendCodeWithTelephoneNumber:self.customPhoneNumber Success:^(NSDictionary * _Nonnull res) {
        if ([res[@"msg"] isEqualToString:@"success"]) {
            self.realCode = res[@"data"][@"code"];
            addAdressCell *Cell = [self.tableView cellForRowAtIndexPath:index];
            [Cell daojishi];
        }else{
            [self showMBPError:res[@"msg"]];
        }
    } Failure:^(NSError * _Nonnull error) {
        [self showMBPError:[HttpTool handleError:error]];
    }];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 4;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.01f;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return nil;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01f;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return nil;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    addAdressCell *cell = [tableView dequeueReusableCellWithIdentifier:@"addAdressCellID"];
    cell.rightarrow.hidden = YES;
    if (indexPath.section == 0) {
        cell.subtitle.text = @"商户姓名";
        cell.detailtext.placeholder = @"客户姓名";
        if (self.customName.length > 0) {
            cell.detailtext.placeholder = self.customName;
        }
    }else if (indexPath.section == 1) {
        cell.subtitle.text = @"联系方式";
        cell.detailtext.placeholder = @"输入手机号码";
        if (self.customPhoneNumber.length > 0) {
            cell.detailtext.placeholder = self.customPhoneNumber;
        }
    }else if (indexPath.section == 2) {
        cell.subtitle.text = @"验证码";
        cell.detailtext.placeholder = @"输入验证码";
        if (self.code.length > 0) {
            cell.detailtext.placeholder = self.code;
        }
        cell.sendbutton.hidden = NO;
        [cell.sendbutton setTitle:@"获取验证码" forState:UIControlStateNormal];
        [cell setSendBlock:^{
            if ([self.customPhoneNumber isVaildMobileNo]) {
                [self sendCodeWith:indexPath];
            }else{
                [self showMBPError:@"请输入正确的手机号"];
            }
        }];
    }else if (indexPath.section == 3) {
        cell.subtitle.text = @"商户编号";
        cell.detailtext.placeholder = @"输入机器序列号(SN)";
        if (self.posNnstanceSn.length > 0) {
            cell.detailtext.text = self.posNnstanceSn;
        }
        cell.sendbutton.hidden = NO;
        [cell.sendbutton setTitle:@"自动识别" forState:UIControlStateNormal];
    }
    [cell setDetailBlock:^(NSString * _Nonnull str) {
        if (indexPath.section == 0) {
            self.customName = str;
        }else if (indexPath.section == 1) {
            self.customPhoneNumber = str;
        }else if (indexPath.section == 2) {
            self.code = str;
        }else if (indexPath.section == 3) {
            self.posNnstanceSn = str;
        }
    }];
    return cell;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    for (UITableViewCell *cell in self.tableView.visibleCells) {
        if ([cell isKindOfClass:[addAdressCell class]]) {
            addAdressCell *SCell = (addAdressCell *)cell;
            [SCell.detailtext resignFirstResponder];
        }
    }
}

@end
